﻿using LS.Nc6.Web.Entry.ViewModel.UI;

namespace LS.Nc6.Web.Entry.ViewComponents
{
    public class ProductViewComponent : ViewComponent
    {
        private readonly ICmsChannelService _cmsChannelService;
        private readonly ICmsContentService _contentService;
        private readonly ISysFileService _fileService;
        public ProductViewComponent(ICmsChannelService cmsChannelService , ICmsContentService contentService, ISysFileService sysFileService)
        {
            _cmsChannelService= cmsChannelService;
            _contentService = contentService;
            _fileService = sysFileService;
        }
        public  async Task<IViewComponentResult> InvokeAsync(long id,int pageIndx)
        {
            CmsChannelListVm cmschannel = new CmsChannelListVm();
            List<CmsContentVm> itemlist = new List<CmsContentVm>();
            cmschannel.PageSize = 9;
            cmschannel.PageIndx = pageIndx;
            var result = await _contentService.PageDynamicList(a => a.ChannelId == id, pageIndx, cmschannel.PageSize, true, a => a.AddTime, a => new {
                id = a.Id,
                title = a.Title,
                top = a.Top,
                recommend = a.Recommend,
                hot = a.Hot,
                color = a.Color,
                channelId = a.ChannelId,
                createDate = a.AddTime,
                userName = a.UserName,
                checkedState = a.Checked,
                summary = a.Summary,
                subTitle = a.SubTitle,
                addTime = a.AddTime,
                imageUrlIds = a.ImageUrlIds
            });
            foreach (var item in result.Item1)
            {
                itemlist.Add(new CmsContentVm
                {
                    Id = item.id,
                    Title = item.title,
                    Summary = item.summary,
                    SubTitle = item.subTitle,
                    AddTime = Convert.ToDateTime(item.addTime).ToString("yyyy-MM-dd"),
                    ContentBackground = !string.IsNullOrEmpty(item.imageUrlIds) ? (await _fileService.GetByIdAsync(CommonUtil.GetLongList(item.imageUrlIds)[0]))?.Fpath : "ui/image/product.png"
                });
            }
            cmschannel.Id = id;
            cmschannel.TotalRow = result.Item2;
            cmschannel.ContentList = itemlist;
            return View(cmschannel);
        }
    }
}
